package aom.ju.ss.view.cut

import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.CutIngListActivityBinding
import aom.ju.ss.databinding.CutListItem2Binding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.AllCutMode
import aom.ju.ss.mode.AllCutModeData
import aom.ju.ss.utils.Fin
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.utils.start
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.PAdapter
import com.google.gson.Gson

/**
 * 砍价中的订单
 */
class CutIngListActivity : BaseActivity() {
    private lateinit var binding: CutIngListActivityBinding

    private lateinit var pAdapter: PAdapter
    private var list = arrayListOf<AllCutModeData>()
    private var page = 1
    override fun init() {
        binding = viewBinding as CutIngListActivityBinding
        pAdapter = PAdapter(
            list, R.layout.cut_list_item2
        ) { bin, position ->
            val b = bin as CutListItem2Binding
            b.info = list[position]
            b.cut.setOnClickListener {
//                if (list[position].is_help==1){
//                    return@setOnClickListener
//                }
                start(
                    HelpCutDetailActivity::class.java,
                    list[position].id.toString()
                )
            }
        }
        binding.mRecycler.layoutManager = LinearLayoutManager(activity)
        binding.mRecycler.adapter = pAdapter

        binding.small.setOnLoadMoreListener {
            page++
            getData
        }
        binding.small.setOnRefreshListener {
            page = 1
            getData
        }
        getData

    }

    val getData: Unit
        get() {
            OKHttpUtils(activity)
                .SetApiUrl(LP_API.cut_order_get_list)
                .SetKey("page")
                .SetValue(page.toString())
                .POST(object : MyHttpCallBack {
                    override fun ok(jsonString: String?, httpTY: String?) {
                        val mode = Gson().fromJson(jsonString, AllCutMode::class.java)
                        if (page == 1) {
                            list.clear()
                        }
                        mode.data?.let {
                            list.addAll(it)
                        }
                        pAdapter.notifyDataSetChanged()
                        binding.small.Fin()
                    }

                    override fun error(e: String?, jsonString: String?, httpTY: String?) {

                        binding.small.Fin()
                        e?.show
                    }

                })
        }

    override val lay: Int = R.layout.cut_ing_list_activity
}